﻿using System;
using System.Text;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.Types;
using System.Collections.Generic;
using OpenExpressApp.MetaModel;
using System.Linq;
using System.Text.RegularExpressions;

namespace OpenExpressApp.Command
{
    /// <summary>
    /// 所有命令的基类
    /// </summary>
    public abstract class CommandBase
    {
        //用来扩充参数用
        public readonly Dictionary<string, object> CustomParams = new Dictionary<string, object>();
        public CommandBase()
        {
            var thisType = this.GetType();
            if (thisType.HasMarked<CommandAttribute>())
            {
                CommandAttribute commandAttribute = thisType.GetSingleAttribute<CommandAttribute>();
                this.Id = commandAttribute.Id;
                this.Label = commandAttribute.Label ?? this.Id;

                this.ToolTip = commandAttribute.ToolTip;
                this.TargetViewType = commandAttribute.TargetViewType;
                this.ToolbarType = commandAttribute.ToolbarType;
                this.ModuleType = commandAttribute.ModuleType;
                this.TargetObjectType = commandAttribute.TargetObjectType;
                this.Category = commandAttribute.Category;
                this.Group = commandAttribute.Group;
                this.CommandType = commandAttribute.CommandType;
                this.IngoreIsExecuting = commandAttribute.IngoreIsExecuting;
                this.ImageName = commandAttribute.ImageName;
            }
        }

        #region 属性：标记在CommandAttribute的对应的值

        public Type TargetObjectType { get; private set; }
        public ModuleType ModuleType { get; private set; }
        public ViewType TargetViewType { get; private set; }
        public ToolbarType ToolbarType { get; private set; }
        public CommandCategory Category { get; private set; }
        public string Group { get; private set; }
        public CommandType CommandType { get; private set; }
        public string Id { get; private set; }
        public string Label { get; private set; }
        public string ToolTip { get; private set; }
        public string ImageName { get; private set; }
        public bool IngoreIsExecuting { get; private set; }

        #endregion

        /// <summary>
        /// 编程时可使用的名字，
        /// “由于Name可能包含一些奇怪的字符，所以不能直接赋值给控件的Name”
        /// </summary>
        public string ProgramingName
        {
            get
            {
                string result = Regex.Replace(this.Id, @"[^\w_]", "_");
                if (Regex.Match(result, @"^\d").Success)
                {
                    result = result.Insert(0, "name_");
                }
                return result;
            }
        }

        /// <summary>
        /// 是否需要显示这个命令所对应的按钮
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual bool CanVisible(object param)
        {
            return true;
        }

        /// <summary>
        /// 是否这个命令所对应的按钮可以被执行
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual bool CanExecute(object param)
        {
            return true;
        }
        /// <summary>
        /// 执行这个命令
        /// </summary>
        /// <param name="param"></param>
        public abstract void Execute(object param);
    }

    public abstract class ViewCommand : CommandBase
    {
        public string ParametrizedValue
        {
            get
            {
                string value = string.Empty;
                object obj = null;
                if (CustomParams.TryGetValue(CommandCustomParams.ParametrizedValue, out obj))
                {
                    value = Convert.ToString(obj);
                }
                return value;
            }
        }
    }
}